/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 *
 * @author Progresywny Maciek
 */
public class PicturePanel extends JPanel {

    private BufferedImage img = new BufferedImage(32,32,BufferedImage.TYPE_3BYTE_BGR);
    private Point p1;
    private Point p2;
    private Rectangle[] rect;
    
    public PicturePanel() {
    }

    public void repaintImage() {
        repaint();
    }

    public void setImage(BufferedImage image) {
        if(image!=null){
        img = Utils.deepCopy(image);
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Image drawImage;
        if (img != null) {
            drawImage = img;
            g.drawImage(drawImage, 1, 1, null);
        }
        if (p1 != null && p2 !=null) {
            paintLine(g, p1 ,p2);
        }
        if (rect != null) {
            paintRect(g, rect);
        }
    }

    private void paintLine(Graphics g, Point p1, Point p2) {
            g.setColor(Color.ORANGE);
            g.drawLine(p1.x,p1.y,p2.x,p2.y); 
    }

    private void paintRect(Graphics g, Rectangle[] rect) {
        for (Rectangle r : rect) {
            g.drawRect((int) r.getX(), (int) r.getY(), (int) r.getWidth(), (int) r.getHeight());
        }
    }

    public void setRectagles(Rectangle[] rectangles) {
        this.rect = rectangles;
    }

    public void setLines(Point p1, Point p2) {
        this.p1 = p1;
        this.p2 = p2;
    }
}
